{% extends "ik/base.html" %}

{% block head_interior %}
	<script language="javascript" src="http://o.aolcdn.com/dojo/0.4.3/dojo.js"></script>

	<link rel="stylesheet" type="text/css" href="/static/ik/news.css" />
{% endblock %}

{% block title %}Inselkampf News{% endblock %}

{% block main_content %}
	<h1>Inselkampf News</h1>
	<p>Last scan: {{ scan.end_time|date:"jS o\f F, P" }}</p>

	<h2>Score Change In Last Day</h2>
	<div id="allianceScoreChart"></div>

	<script type="text/javascript"><!--
	google_ad_client = "pub-7423629205927780";
	google_alternate_color = "ffffff";
	google_ad_width = 728;
	google_ad_height = 90;
	google_ad_format = "728x90_as";
	google_ad_type = "text_image";
	google_ad_channel = "";
	google_color_border = "FFFFFF";
	google_color_bg = "FFFFFF";
	google_color_link = "0616f3";
	google_color_text = "000000";
	google_color_url = "6bb6e1";
	//-->
	</script>
	<script type="text/javascript"
	  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
	</script>

	<h2>Captured Islands</h2>
	{% for mapscan in latest_scans %}
	<h3>{{ mapscan.scan.end_time|timesince }} ago</h3>
	<table>
		<thead>
			<th>New Owner</th>
			<th>Alliance</th>
			<th>Took This Island</th>
			<th>From</th>
			<th>Alliance</th>
			<th>Score</th>

		</thead>

		<tbody>
			{% for capture in mapscan.captures %}
			<tr>
				<td><a href={{capture.new_player.get_absolute_url}}>{{capture.new_player.name}}</a></td>
				<td><a href={{capture.new_player.alliance.get_absolute_url}}>{{capture.new_player.alliance.tag}}</a></td>
				<td><a href={{capture.island.get_absolute_url}}>{{capture.island.position}}</a></td>
				<td><a href={{capture.old_player.get_absolute_url}}>{{capture.old_player.name}}</a></td>
				<td><a href={{capture.old_player.alliance.get_absolute_url}}>{{capture.old_player.alliance.tag}}</a></td>
				<td>{{capture.island.score}}</td>
			</tr>
			{% endfor %}
		</tbody>
	</table>
	{% endfor %}

	<script type="text/javascript"><!--
	google_ad_client = "pub-7423629205927780";
	google_alternate_color = "ffffff";
	google_ad_width = 200;
	google_ad_height = 90;
	google_ad_format = "200x90_0ads_al_s";
	google_ad_channel = "";
	google_color_border = "FFFFFF";
	google_color_bg = "FFFFFF";
	google_color_link = "6BB6E1";
	google_color_text = "000000";
	google_color_url = "0616f3";
	//-->
	</script>
	<script type="text/javascript"
	  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
	</script>

	<h2>Abandoned Islands</h2>
	<table>
		<thead>
			<th>Island</th>
			<th>Position</th>
			<th>Score</th>
		</thead>
	{% for isle in newly_rulerless %}
		<tr>
			<td>{{ isle.name }}</td>
			<td><a href={{isle.get_absolute_url}}>{{ isle.position }}</a></td>
			<td>{{ isle.score }}</td>
		</tr>
	{% endfor %}
	</table>

	<h2>New Islands</h2>
	<table>
		<thead>
			<th>Island</th>
			<th>Position</th>
			<th>Ruler</th>
			<th>Alliance</th>
			<th>Score</th>
		</thead>
	{% for isle in new_isles %}
		<tr>
			<td>{{ isle.name }}</td>
			<td><a href={{isle.get_absolute_url}}>{{ isle.position }}</a></td>
			<td><a href={{isle.island.player.get_absolute_url}}> {{ isle.island.player.name }} </a> </td>
			<td><a href={{isle.island.player.alliance.get_absolute_url}}> {{ isle.alliance_tag }} </a> </td>
			<td>{{ isle.score }}</td>
		</tr>
	{% endfor %}
	</table>

<script language="javascript">
	dojo.require("dojo.collections.Store");
	dojo.require("dojo.charting.Chart");
	dojo.require('dojo.json');

	function buildAxisX(data)
	{
		//Find the bounds
		low_rank = data[0].rank;
		high_rank = data[0].rank;
		for( i=0 ; i<data.length ; i++)
		{
			var obj = data[i];

			//If lower
			if( obj.rank < low_rank)
				low_rank = obj.rank;

			//If higher
			if( obj.rank > high_rank)
				high_rank = obj.rank;
		}

		var xA = new dojo.charting.Axis();
		xA.range={upper:high_rank+1, lower:low_rank-1};
		xA.origin=0;
		xA.showTicks = true;
		xA.showLabel = false;
		xA.label = "Alliances";

		xA.labels = []
		for( i=0; i<data.length ; i++)
		{
			var obj = data[i];
			xA.labels.push({label:obj.tag , value:obj.rank});
		}
		return xA;
	}

	function buildAxisY(data)
	{
		//Always display the origin
		high = 0;
		low = 0;
		for( i=0 ; i<data.length ; i++)
		{
			var obj = data[i];

			//If higher
			if( obj.delta > high)
				high = obj.delta;

			if( obj.delta < low)
				low = obj.delta;
		}

		var yA = new dojo.charting.Axis();
		yA.range={upper: high,lower:low};
		yA.showLines = true;
		yA.showTicks = true;
		yA.showLabel = false;
		yA.label = "Score Change"

		//5 tick marks
		yA.labels = []
		var nMarks = 5;
		for( i=0; i<(nMarks-1) ; i++)
		{
			var score = low + Math.floor( (high-low)*(i/(nMarks-1)) );
			yA.labels.push( {label: score+'', value:score } );
		}
		//Now put the last on
		yA.labels.push( {label: high+'', value:high} );

		return yA;
	}

	function addAlliances(chart, plotArea, data)
	{
		//Calculate change in score
		for( i=0; i<data.length ; i++ )
		{
			var item = data[i];
			item.delta = item.score - item.prev_score;
		}

		var store = new dojo.collections.Store();
		store.setData(data);

		var delta_series = new dojo.charting.Series({
			dataSource : store,
			bindings : { x:"rank", y:"delta" },
			label : "Change"
			});

		var xA = buildAxisX(data);
		var yA = buildAxisY(data);

		var p = new dojo.charting.Plot(xA, yA);
		p.addSeries({ data:delta_series, plotter: dojo.charting.Plotters.DataBar });

		plotArea.plots.push(p);
		delta_series.color = "#0616f3";

		chart.render();

	}

	function onLoad()
	{
				var pa = new dojo.charting.PlotArea();
				pa.size={width:680, height:200};
				pa.padding={top:20, right:20, bottom:30, left:50 };


				var chart = new dojo.charting.Chart(null, "Alliance Score Delta", "The change in score in the last 24 hours.");
				//The x and y values are required, or the area wont show in IE.
				chart.addPlotArea({x:0, y:0, plotArea:pa });

				chart.node = dojo.byId("allianceScoreChart");
				chart.render();

				var bindArgs = {
									url: "/ik/ajax/alliance_info_list/",
									error: function(type, error, evt){ alert(error.message); },
									load: function(type, data, evt) {addAlliances(chart,pa,data); },
									mimetype: "text/json"
								};

				dojo.io.bind(bindArgs);
	}
	dojo.addOnLoad( function() { onLoad(); });



</script>

{% endblock %}

